var express = require('express');
var request = require('request');
var petServer=require('../chachaServer/chachaServer')
var sign = require('../sign.js');
var acc_token=''
var acc_tik=''

get_token_ticket()
setInterval(function(){
    get_token_ticket()
},7000000)
//var multiparty = require('multiparty');

//var fs = require('fs');
var router = express.Router();
/* GET users listing. */

router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.post('/add', function(req, res, next) {
  petServer.add(req, res, next)
});

router.post('/updateInfo', function(req, res, next) {
  petServer.updateInfo(req, res, next)
});
router.post('/remove', function(req, res, next) {
    petServer.remove(req, res, next)
});

router.get('/getList', function(req, res, next) {
  petServer.getList(req, res, next)
});
//var calcSignature = function (ticket, noncestr, ts, url) {
//    var str = 'jsapi_ticket=' + ticket + '&noncestr=' + noncestr + '&timestamp='+ ts +'&url=' + url;
//    shaObj = new jsSHA(str, 'TEXT');
//    return shaObj.getHash('SHA-1', 'HEX');
//}
//var signature = calcSignature(ticket, noncestr, timestamp, url);
function get_token_ticket(){
    request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4ea8199a93618609&secret=7aca6d3596d8b7cdb50fac8eb6bd81ba', function (error, response, body) {
       // console.log('error:', error); // Print the error if one occurred
       // console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        //console.log('body:', body.access_token); // Print the HTML for the Google homepage.
        var data=JSON.parse(body)
        //console.log(data.access_token)
        acc_token=data.access_token
        request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='+acc_token+'&type=jsapi', function (error, response, body) {
            //console.log('error:', error); // Print the error if one occurred+
            //console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
            //console.log('body:', body.access_token); // Print the HTML for the Google homepage.
            var data=JSON.parse(body)
            //console.log(data)
            acc_tik=data.ticket


           // res.send({message:'true',data:sign('jsapi_ticket', 'http://example.com')} );
        });
    });
}
router.get('/getAllInfo',function(req,res,next){
  // console.log('get all info')
    var param=req.query || req.params;
   // console.log(req.body,req.query,req.params,acc_tik,param.url )
    res.send({message:'true',data:sign(acc_tik, param.url)} );
  //https.get('http://118.178.235.122:8000/bot/api/v0.1/itemquery?uid=&s=&st=1&by=price&pn=1&ps=10&q=%E4%BD%A0%E5%A5%BD&pid=455215', function(data) {
    //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxaa1f342551a5902b&secret=5ab88302417be1e1a47daa33c50eeee5
    // 这个异步回调里可以获取access_token

  //})
})
router.get('/getInfo', function(req, res, next) {
  petServer.getInfo(req, res, next)
});
router.post('/login', function(req, res, next) {
  petServer.login(req, res, next)
});
router.post('/addReview', function(req, res, next) {
  petServer.addReview(req, res, next)
});
router.post('/addPower', function(req, res, next) {
  petServer.addPower(req, res, next)
});
router.get('/register', function(req, res, next) {
  petServer.register(req, res, next);
});

router.post('/upload', function(req, res, next) {
  //res.send({data: "  var data='name':'yuqj'  "});
  //console.log(req.body,req.body.petName);
  //chachaServer.intoPetData(req, res, next);


  res.json({data: "  var data='name':'yuqj'  "});
 // var form = new multiparty.Form({uploadDir: './public/images/'});
 //    //上传完成后处理
 //    form.parse(req, function(err, fields, files) {
 //        var filesTmp = JSON.stringify(files,null,2);
 //        if(err){
 //            console.log('parse error: ' + err);
 //          res.writeHead(200, {"Content-Type": "application/json"});
 //          res.write(err);
 //          res.end();
 //          } else {
 //            console.log('parse files: ' + filesTmp);
 //          res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
 //          console.log('dkdk')
 //          //res.writeHead(200, {"Content-Type": "application/json"});
 //          res.write(filesTmp);
 //          console.log('dkdk')
 //          res.end();
 //
 //        }
 //        //    var inputFile = files.inputFile[0];
 //        //    var uploadedPath = inputFile.path;
 //        //    var dstPath = './public/images/' + inputFile.originalFilename;
 //        //    //重命名为真实文件名
 //        //    fs.rename(uploadedPath, dstPath, function(err) {
 //        //        if(err){
 //        //            console.log('rename error: ' + err);
 //        //          } else {
 //        //            console.log('rename ok');
 //        //          }
 //        //      });
 //        //}
 //
 //
 //     });
});
module.exports = router;
